- base: old system
- lattice: creating function to plot
- ggplot2
index<-data.frame(year=2007:2016, pop=sample(10000:20000, size=10)) plot(index$year,index$pop)
#lines plot(index$year,index$pop, type="l")
plot(index$year,index$pop, type="h")
plot(index$year,index$pop, type="l", main="Population by year",
xlab="year",ylab="population")
lattice::dotplot(year~pop,data=index,main="Population by year",
ylab="year",xlab="population")
index$year<-as.factor(index$year)
lattice::dotplot(year~pop,data=index,main="Population by year",
ylab="year",xlab="population")
histogram(year~pop,data=index)
##Lattice - more complex formula
xyplot(mpg~wt | factor(cyl), data=mtcars, pch=19,
main="MPG vs Wt", xlab="Wt/1,000", ylab="MPG")
Foundation for many graphics applications:
ggplot(data=faithful,
mapping=aes(x=eruptions,
y=waiting))+
geom_point()
ggplot(data=faithful)+
geom_point(mapping=aes(x=eruptions,
y=waiting))
ggplot()+
geom_point(data=faithful,mapping=aes(x=eruptions,
y=waiting))
ggplot(data=faithful)+
geom_point(mapping=aes(x=eruptions,y=waiting,
colour=eruptions<3))
ggplot(data=faithful)+ geom_histogram(mapping=aes(x=eruptions))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data=faithful,
mapping=aes(x=eruptions,
y=waiting))+
geom_density_2d()+
geom_point()
ggplot(mpg) + geom_bar(aes(x = class))
mpg_counted <- mpg %>%
count(class, name = 'count')
ggplot(mpg_counted) +
geom_bar(aes(x = class,
y = count),
stat = 'identity')ggplot(mpg) +
geom_bar(aes(x = class,
y = after_stat(100 * count /
sum(count))))Values calculated by the stat is available with the after_stat() function inside aes()
ggplot(mpg) +
geom_point(
aes(x = displ,
y = hwy,
colour = class))aes() should happen. All mappings have an associated scale even if not specifiedggplot(mpg) + geom_point(aes(x = displ, y = hwy, colour = class)) + scale_colour_brewer(type = 'qual')
ggplot(mpg) + geom_point(aes(x = displ, y = hwy)) + scale_x_continuous(breaks = c(3, 5, 6)) + scale_y_continuous(trans = 'log10')
facet_null()) puts all the data in a single panelfacet_wrap() and facet_grid() allows you to specify different types of small multiplesggplot(mpg) + geom_point(aes(x = displ, y = hwy)) + facet_wrap(~ class)
ggplot(mpg) + geom_bar(aes(x = class)) + coord_polar()
ggplot(mpg) + geom_bar(aes(x = class)) + coord_polar(theta = 'y') + expand_limits(y = 70)
ggplot(mpg) + geom_bar(aes(x = class)) + scale_y_continuous(limits = c(0, 40))
ggplot(mpg) + geom_bar(aes(x = class)) + coord_cartesian(ylim = c(0, 40))
scale<-ggplot(diamonds, aes(carat, price)) + geom_point() + scale_x_log10() + scale_y_log10() coord<-ggplot(diamonds, aes(carat, price)) + geom_point() + coord_trans(x = "log10", y = "log10") (scale | coord)
ggplot(mpg) + geom_bar(aes(y = class)) + facet_wrap(~year) + theme_minimal()
ggplot(mpg) +
geom_bar(aes(y = class)) +
facet_wrap(~year) +
labs(title = "Number of car models per class",
caption = "source: http://fueleconomy.gov",
x = NULL,
y = NULL) +
scale_x_continuous(expand = c(0, NA)) +
theme_minimal() +
theme(
text = element_text('Avenir Next Condensed'),
strip.text = element_text(face = 'bold', hjust = 0),
plot.caption = element_text(face = 'italic'),
panel.grid.major = element_line('white', size = 0.5),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank(),
panel.ontop = TRUE
)
library(esquisse) esquisser(airquality)
plot composition:
(p1 | p2) / p3
p_all <- (p1 | p2) /
p3
p_all + plot_layout(guides = 'collect')
Patchwork will assign the same amount of space to each plot by default, but this can be controlled with the widths and heights argument in plot_layout(). This can take a numeric vector giving their relative sizes (e.g. c(2, 1) will make the first plot twice as big as the second). Modify the code below so that the middle plot takes up half of the total space:
p <- ggplot(mtcars) + geom_point(aes(x = disp, y = mpg)) p + p + p
ggplot(economics) + geom_line(aes(x = date, y = unemploy))
ggplot(economics) + geom_line(aes(x = date, y = unemploy)) + transition_reveal(along = date)
ggplot(mpg) + geom_bar(aes(x = factor(cyl))) +
labs(title = 'Number of cars in {closest_state} by number of cylinders') +
transition_states(states = year) + enter_grow() +
exit_fade()